package TAIC.flicker.Util;

import java.io.File;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Vector;

public class WordCount {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		if ( args.length != 2 ) {
			System.out.println ( "Please input the taggingFile and outputFile" ) ;
			return ;
		}
		HashMap < String , Vector<String> > map = new HashMap < String, Vector<String>> () ;
		
		PrintStream fout = new PrintStream ( new File ( args [ 1 ])); 
		Scanner scanner = new Scanner ( new File ( args [ 0 ]));
		Vector < String > vec ;
		while ( scanner.hasNext() ){
			String fileName = scanner.next() ;
			fileName = fileName.substring(0, fileName.length() - 4 ) ; 
			scanner.next() ;
			String line = scanner.nextLine () ;
			Scanner lineScanner = new Scanner ( line ) ;
			while ( lineScanner.hasNext()) {
				String tmp = lineScanner.next() ;
				if ( (vec = map.get(tmp))== null ) vec = new Vector < String >() ; 
				vec.add(fileName) ;
				map.put(tmp, vec) ;
			}
		}
		scanner.close();
		
		for ( Map.Entry<String, Vector<String>>e : map.entrySet() ) {
			fout.print (e.getKey() + " " + e.getValue().size() ) ;
			for ( String str : e.getValue()) fout.print( " " + str ) ; 
			fout.println () ;
		}
		fout.close() ;
	}

}
